package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.ProjectDetailsRaceTypeSamsaraMapper;
import com.jdz.dao.RaceTypeDetailsMapper;
import com.jdz.dao.RaceTypeMapper;
import com.jdz.dao.RaceTypeSamsaraMapper;
import com.jdz.pojo.RaceType;
import com.jdz.pojo.RaceTypeDetails;
import com.jdz.pojo.RaceTypeSamsara;
import com.jdz.service.IRaceTypeDetailsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service("IRaceTypeDetailsService")
public class RaceTypeDetailsServiceImpl implements IRaceTypeDetailsService {

    private Logger logger = LoggerFactory.getLogger(RaceTypeDetailsServiceImpl.class);

    @Autowired
    RaceTypeMapper raceTypeMapper;
    @Autowired
    RaceTypeDetailsMapper raceTypeDetailsMapper;
    @Autowired
    RaceTypeSamsaraMapper raceTypeSamsaraMapper;
    @Autowired
    ProjectDetailsRaceTypeSamsaraMapper projectDetailsRaceTypeSamsaraMapper;

    /**
     * 列出项目类型详情和当前类型处以比赛的等级
     * @param racetypeno
     * @return
     */
    public ServerResponse details(Integer racetypeno){
        if(racetypeno==null){
            return ServerResponse.createBySuccessMessage("没有参数");
        }
        RaceTypeDetails raceTypeDetails = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);
        if(raceTypeDetails==null){
            Map<String ,Object> map = new HashMap<>();
            map.put("name",raceTypeMapper.selectByPrimaryKey(racetypeno).getRacetype());
            return ServerResponse.createBySuccess("没有项目详情",map);
        }

        Map<String ,Object> map = new HashMap<>();
        map.put("name",raceTypeMapper.selectByPrimaryKey(racetypeno).getRacetype());
        map.put("value",raceTypeDetails);
        return ServerResponse.createBySuccess("成功返回项目详情",map);
       // return ServerResponse.createBySuccess("成功返回项目详情",raceTypeDetails);
    }

    /**
     * 添加项目类型详情
     * @param racetypeno
     * @param racetypedetailed
     * @return
     */
    public ServerResponse add(Integer racetypeno,String racetypedetailed){

        if(racetypeno==null||racetypeno==0|| StringUtils.isBlank(racetypedetailed)){
            return ServerResponse.createByErrorMessage("参数不对");
        }

        RaceTypeDetails raceTypeDetailspanduanm = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);
        if(raceTypeDetailspanduanm!=null){
            return ServerResponse.createByErrorMessage("已有此项目详情，添加失败");
        }

        RaceTypeDetails raceTypeDetails = new RaceTypeDetails();
        raceTypeDetails.setRacetypeno(racetypeno);
        raceTypeDetails.setRacetypedetailed(racetypedetailed);
        int row = raceTypeDetailsMapper.insertSelective(raceTypeDetails);
        if(row <= 0){
            return ServerResponse.createByErrorMessage("添加此项目类型详情失败");
        }
        return ServerResponse.createBySuccessMessage("添加此项目类型详情成功");
    }

    /**
     * 修改项目类型详情
     * @param racetypeno
     * @param racetypedetailed
     * @return
     */
    public ServerResponse update(Integer racetypeno,String racetypedetailed){
        if(racetypeno==null||racetypeno==0|| StringUtils.isBlank(racetypedetailed)){
            return ServerResponse.createByErrorMessage("参数不对");
        }

        int row = raceTypeDetailsMapper.updateByRacetypeno(racetypeno,racetypedetailed);
        if(row <= 0){
            return ServerResponse.createByErrorMessage("修改此项目类型详情失败");
        }
        return ServerResponse.createBySuccessMessage("修改此项目类型详情成功");
    }

    /**
     * 开启下一轮比赛
     * @param racetypeno
     * @return
     */
    public ServerResponse nextLevel(Integer racetypeno){

        if(racetypeno==null){
            return ServerResponse.createByErrorMessage("参数不对");
        }
        RaceType raceType = raceTypeMapper.selectByPrimaryKey(racetypeno);
        if(raceType==null){
            return ServerResponse.createByErrorMessage("该项目类型不存在");
        }
        raceType.setStatus(1);
        int i = raceTypeMapper.updateByPrimaryKeySelective(raceType);
        if(i<=0){
            return ServerResponse.createByErrorMessage("该项目类型出错");
        }


        List<RaceTypeSamsara> r = raceTypeSamsaraMapper.selectByRacetypeno(racetypeno);

        if(CollectionUtils.isEmpty(r)){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("还没有比赛轮次");
        }
        RaceTypeDetails raceTypeDetailsRow  = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);

        if(raceTypeDetailsRow==null){
            RaceTypeDetails raceTypeDetails = new RaceTypeDetails();
            raceTypeDetails.setRacetypeno(racetypeno);
            raceTypeDetails.setRacetypedetailsstate(1);

            int rulth = raceTypeDetailsMapper.insertSelective(raceTypeDetails);

            if(rulth <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("开启下一轮比赛失败");
            }
            return ServerResponse.createBySuccessMessage("开启下一轮比赛成功");
        }

        if(raceTypeDetailsRow.getRacetypedetailsstate() >=  r.get(r.size()-1).getRacetypesamsaragrade()){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("操作失败，已经是最后一轮比赛了");
        }

        if(raceTypeDetailsRow.getRacetypedetailsstate()==-1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("比赛已结束");
        }


        int row = raceTypeDetailsMapper.updateByRacetypenoState(racetypeno, raceTypeDetailsRow.getRacetypedetailsstate()+1);

        if(row <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("开启下一轮比赛失败");
        }
        return ServerResponse.createBySuccessMessage("开启下一轮比赛成功");
    }

    /**
     * 设置比赛轮次
     * @param racetypeno
     * @return
     */
    public ServerResponse setLevel(Integer racetypesamsarano,Integer racetypeno){
        if(racetypeno==null||racetypeno==0||racetypesamsarano==null||racetypesamsarano==0){
            return ServerResponse.createByErrorMessage("参数不对");
        }

        RaceType raceType = raceTypeMapper.selectByPrimaryKey(racetypeno);
        if(raceType==null){
            return ServerResponse.createByErrorMessage("该项目类型不存在");
        }
        raceType.setStatus(1);
        int i = raceTypeMapper.updateByPrimaryKeySelective(raceType);
        if(i<=0){
            return ServerResponse.createByErrorMessage("该项目类型出错");
        }

        List<RaceTypeSamsara> r = raceTypeSamsaraMapper.selectByRacetypeno(racetypeno);

        //获取项目等级详情
        RaceTypeSamsara raceTypeSamsara = raceTypeSamsaraMapper.selectByPrimaryKey(racetypesamsarano);

        if(raceTypeSamsara==null){
            return ServerResponse.createByErrorMessage("找不到该等级");
        }

        RaceTypeDetails raceTypeDetails = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);

        if(raceTypeDetails==null){
            RaceTypeDetails raceTypeDetailsVo = new RaceTypeDetails();
            raceTypeDetailsVo.setRacetypeno(racetypeno);
            raceTypeDetailsVo.setRacetypedetailsstate(raceTypeSamsara.getRacetypesamsaragrade());

            int rulth = raceTypeDetailsMapper.insertSelective(raceTypeDetailsVo);

            if(rulth <= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("设置比赛轮次失败");
            }
            return ServerResponse.createBySuccessMessage("设置比赛轮次成功，自动关闭申报");
        }

        int row = raceTypeDetailsMapper.updateByRacetypenoState(racetypeno,raceTypeSamsara.getRacetypesamsaragrade());

        if(row <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("设置比赛轮次失败");
        }
        return ServerResponse.createBySuccessMessage("设置比赛轮次成功，自动关闭申报");
    }

    public ServerResponse stopLevel(Integer racetypeno){
        if(racetypeno==null||racetypeno==0){
            return ServerResponse.createByErrorMessage("参数不对");
        }

        RaceTypeDetails raceTypeDetails = raceTypeDetailsMapper.selectByRacetypeno(racetypeno);

        if(raceTypeDetails==null){
            RaceTypeDetails raceTypeDetailsVo = new RaceTypeDetails();
            raceTypeDetailsVo.setRacetypeno(racetypeno);
            raceTypeDetailsVo.setRacetypedetailsstate(-1);

            int rulth = raceTypeDetailsMapper.insertSelective(raceTypeDetailsVo);

            if(rulth <= 0){
                return ServerResponse.createByErrorMessage("结束比赛轮次失败");
            }
            return ServerResponse.createBySuccessMessage("结束比赛轮次成功");
        }

        int row = raceTypeDetailsMapper.updateByRacetypenoState(racetypeno,-1);

        if(row <= 0){
            return ServerResponse.createByErrorMessage("结束比赛轮次失败");
        }
        return ServerResponse.createBySuccessMessage("结束比赛轮次成功");

    }

}
